package com.solution.service;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;

import com.solution.constant.ErrorMessages;
import com.solution.constant.ExamErrorMessages;
import com.solution.constant.StatusConstants;
import com.solution.dao.CategoryTypeDAO;
import com.solution.dao.DAOFactory;
import com.solution.exception.DAOException;
import com.solution.exception.ServiceBeanException;
import com.solution.model.CategoryType;
import com.solution.util.SearchVO;

@Stateless
@Remote(CategoryTypeService.class)
@RemoteBinding(jndiBinding="CategoryTypeService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CategoryTypeServiceImpl implements CategoryTypeService{

	/**
	 * DAO instance.
	 */
	private CategoryTypeDAO categoryTypeDao = (CategoryTypeDAO)DAOFactory.getDAOInstance(CategoryTypeDAO.class.getName());
	
	/**
	 * Logger instance.
	 */
	private Logger log = Logger.getLogger(CategoryTypeServiceImpl.class.getName());

	public List<CategoryType> fetchActiveCategories() throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:fetchActiveCategories()");
		try{
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
			return categoryTypeDao.search(searchVO);
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public List<CategoryType> getCategories(SearchVO searchVO) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:getCategories()");
		try{
			searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
			return categoryTypeDao.search(searchVO);
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public CategoryType getCategory(Long categoryTypeId) throws ServiceBeanException{
		log.debug("Entering :: CategoryTypeServiceImpl:getCategory()");
		try{
			if(categoryTypeId != null)
				return categoryTypeDao.read(categoryTypeId);
			else
				throw new ServiceBeanException(ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION+" categoryTypeId");
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}

	public CategoryType createCategory(CategoryType category) throws ServiceBeanException {
		log.debug("Entering :: CategoryTypeServiceImpl:createCategory()");
		try{			
			return categoryTypeDao.create(category);			
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.CATEGORY_CREATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
		
		
	}
}
